import * as sinon from 'sinon';
import axios from 'axios';
import { Run } from '../src/index'; // 更新为实际脚本路径
import https from 'https'
let axiosRequestStub: sinon.SinonStub;

// 模拟 axios 请求

describe('runStep', () => {

    beforeEach(() => {

        // 设置环境变量
        process.env['COMMAND'] = 'RUN';
        process.env['codeupSource'] = JSON.stringify({ sign: 'test-sign', data: { projectId: 'test-project-id', connection: 'test-connection', branch: 'test-branch' } });
        process.env['FLOW_PIPELINE_SOURCES'] = JSON.stringify([{ sign: 'test-sign', data: { projectId: 'test-project-id', connection: 'test-connection', commitId: 'test-commit-id' } }]);
        process.env['FLOW_OPENAPI_URL'] = 'http://test-server-url';
        process.env['FLOW_JOB_TOKEN'] = 'test-job-token';
    });

    afterEach(() => {
    });

    it('should call axios POST request with correct parameters when RUN command is provided', async () => {
        const response = {
            data:{
                successful: true
            }
        };
        axiosRequestStub = sinon.stub(axios, 'request').resolves(response);
        const agent = new https.Agent({
            rejectUnauthorized: false // 设置为 false 来忽略证书验证
        });
        Run();

        sinon.assert.calledOnceWithExactly(axiosRequestStub, {
            method: 'POST',
            url: "http://test-server-url/openapi/codeup/api/scanTask/execute",
            params: {
                "projectId": "test-project-id",
                "connectionId": "test-connection",
                "callbackUrl": "http://test-server-url/openapi/jobs/test-job-token/callback",
                "referName": "test-branch",
                "flowJobToken": "test-job-token"
            },
            httpsAgent: sinon.match.any
        })

    });
});
